package com.e3roid.examples;

import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ExpandableListView;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;
import android.app.ExpandableListActivity;

public class Launcher extends ExpandableListActivity {
	
    private static final String KEY = "KEY";
    
	private ExpandableListAdapter adapter;
    private List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    private List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
    
    private static final String[] groupNames = {
    	"Basic", "Event", "Background", "Animation", "Input", "Text", "Physics", "FPS Counter", "Scripting", "Misc"
    };
    
    private static final String[][] childNames = {
    	{
    		"Drawing Shapes",
    		"Drawing a Sprite",
    		"Drawing a HUD",
    		"Drawing a Tile"
    	},
    	{
    		"Moving a Sprite",
    		"Dragging a Sprite",
    		"Removing a Sprite",
    		"Periodic Updates"
    	},
    	{
    		"Drawing Background",
    		"Tiled Background",
    		"Using TMX Tiled Map",
    		"Using Dynamic Map",
    		"Using Large Map"
    	},
    	{
    		"Basic Animation",
    		"Fade In/Out",
    		"Moving Along Y-axis",
    		"Moving Across axis",
    		"Rotate and Scale",
    		"Changing Colors"
    	},
    	{
    		"Analog OnScreen Control",
    		"Digital OnScreen Control",
    		"Multi Touch Control",
    		"Using Soft Input",
    		"Using Audio Input"
    	},
    	{
    		"Hello, World!",
    		"Moving Text",
    		"Changing Text",
    		"Dynamic Text"
    	},
    	{
    		"Using Physics",
    		"Removing Physics Objects",
    		"Applying Linear Velocity",
    		"Bouncing a Shape"
    	},
    	{
    		"Listening FPS",
    		"Limiting FPS",
    	},
    	{
    		"Scripting with Lua",
    		"Using Canvas with Lua",
    		"Scripting with JavaScript"
    	},
    	{
    		"Pause and Exit",
    		"Using XML Layout 1",
    		"Using XML Layout 2",
    		"Using Debug Console"
    	}
    };
    private static final Object[][] activities = {
    	{
    		ShapeExample.class,
    		SpriteExample.class,
    		HUDExample.class,
    		TiledSpriteExample.class
    	},
    	{
    		SpriteMoveExample.class,
    		SpriteDragExample.class,
    		SpriteRemoveExample.class,
    		SceneUpdateExample.class
    	},
    	{
    		BackgroundExample.class,
    		TiledBackgroundExample.class,
    		TMXTiledMapExample.class,
    		TMXTiledMapExample2.class,
    		TMXTiledLargerMapExample.class
    	},
    	{
    		AnimatedSpriteExample.class,
    		AlphaModifierExample.class,
    		AxisModifierExample.class,
    		MoveModifierExample.class,
    		RotateAndScaleModifierExample.class,
    		ColorModifierExample.class
    	},
    	{
    		AnalogControllerExample.class,
    		DigitalControllerExample.class,
    		MultiTouchControllerExample.class,
    		SoftInputExample.class,
    		AudioInputExample.class
    	},
    	{
    		HelloLabelExample.class,
    		LabelExample.class,
    		LabelReplaceExample.class,
    		LabelResizeExample.class
    	},
    	{
    		PhysicsExample.class,
    		PhysicsRemoveExample.class,
    		PhysicsLinearVelocityExample.class,
    		PhysicsBounceExample.class
    	},
    	{
    		FPSListenerExample.class,
    		FPSLimitExample.class,
    	},
    	{
    		ScriptingLuaExample.class,
    		ScriptingLuaCanvasExample.class,
    		ScriptingJavaScriptExample.class
    	},
    	{
    		MenuExitPauseExample.class,
    		XMLLayoutExample.class,
    		XMLLayoutExample2.class,
    		DebugTerminalExample.class
    	}
    };
    
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.requestFullScreen();

		for (int i = 0; i < groupNames.length; i++) {
			String groupName = groupNames[i];
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(KEY, groupName);
            
        	List<Map<String, String>> children = new ArrayList<Map<String, String>>();
        	
        	String[] childName = childNames[i];
        	for (String name : childName) {
                Map<String, String> curChildMap = new HashMap<String, String>();
        		curChildMap.put(KEY, name);
        		children.add(curChildMap);
        	}
        	childData.add(children);
        }
				
		adapter = new SimpleExpandableListAdapter(
				this, 
                groupData,
                android.R.layout.simple_expandable_list_item_1,
                new String[] { KEY },
                new int[] { android.R.id.text1 },
                childData,
                android.R.layout.simple_expandable_list_item_2,
                new String[] { KEY },
                new int[] { android.R.id.text1 }
		);
		this.setListAdapter(adapter);
		this.setContentView(R.layout.main);
	}
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View view, 
			int groupPosition, int childPosition, final long id) {
		Intent intent = new Intent(this, (Class<?>)activities[groupPosition][childPosition]);
		startActivity(intent);
		return super.onChildClick(parent, view, groupPosition, childPosition, id);
	}

	private void requestFullScreen() {
		Window window = this.getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		window.requestFeature(Window.FEATURE_NO_TITLE);
	}
}
